package com.sdry.web.controller.lz;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.lz.DeviceAccount;
import com.sdry.model.lz.DeviceFile;
import com.sdry.model.lz.DeviceFileAttach;
import com.sdry.model.lz.DeviceFileLubricate;
import com.sdry.model.lz.DeviceFileOverhaul;
import com.sdry.model.lz.LzQueryCriteria;
import com.sdry.service.lz.DeviceFileService;
import com.sdry.utils.ResponseUtil;
import com.sdry.utils.StringUtil;

/**
 * @ClassName DeviceFileController
 * @Description 设备档案
 * @Author lz
 * @Date 2018年9月26日 17:01:23
 * @Version 1.0
 */
@Controller
@RequestMapping("/deviceFile")
public class DeviceFileController {

    public static final Integer AFFECT_ROW = 0;
    Long affectRow1 = 0L;
    @Resource
    DeviceFileService deviceFileService;
    /*DeviceFileAttach deviceFileAttach = null;
    DeviceFileLubricate deviceFileLubricate = null;
    DeviceFileOverhaul deviceFileOverhaul = null;*/
    /**
     * 新增设备档案
     * @param response
     * @param deviceFile 设备档案实体
     */
    @RequestMapping("/addDeviceFile")
    public void addDeviceFile(HttpServletResponse response, DeviceFile deviceFile) throws Exception {
    	if (deviceFile != null) {
			if(StringUtil.isEmpty(deviceFile.getOutTime())){
				deviceFile.setOutTime(null);
			}
			if(StringUtil.isEmpty(deviceFile.getUseTime())){
				deviceFile.setUseTime(null);
			}
		}
        Long affectRow1 = deviceFileService.addDeviceFile(deviceFile);
        if (affectRow1 > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow1);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 新增检修
     * @param deviceFileOverhaul
     */
    @RequestMapping("/addDeviceFileOverhaul")
    public void addDeviceFileOverhaul(DeviceFileOverhaul deviceFileOverhaul) {
        deviceFileService.addDeviceFileOverhaul(deviceFileOverhaul);
    }

    /**
     * 新增易耗品
     * @param deviceFileLubricate
     */
    @RequestMapping("/addDeviceFileLubricate")
    public void addDeviceFileLubricate(DeviceFileLubricate deviceFileLubricate) {
        deviceFileService.addDeviceFileLubricate(deviceFileLubricate);
    }

    /**
     * 新增附属设备明细
     * @param deviceFileAttach
     */
    @RequestMapping("/addDeviceFileAttach")
    public void addDeviceFileOverhaul(DeviceFileAttach deviceFileAttach) {
        deviceFileService.addDeviceFileAttach(deviceFileAttach);
    }

    /**
     * 单个删除和批量删除
     * @param response
     * @param idArr id数组
     * @throws Exception
     */
    @RequestMapping("/deleteDeviceFileById")
    public void deleteDeviceFileById(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr) throws Exception {
        for (int i = 0; i < idArr.length; i++) {
            Long affectRow = deviceFileService.deleteDeviceFileById(Long.parseLong(idArr[i]));
            if (affectRow > AFFECT_ROW) {
                ResponseUtil.write(response, affectRow);
            } else {
                ResponseUtil.write(response, AFFECT_ROW);
            }
        }
    }

    /**
     * 根据ID修改设备档案
     * @param response
     * @param deviceFile 选中行的设备
     * @throws Exception
     */
    @RequestMapping("/editDeviceFileById")
    public void editDeviceFileById(HttpServletResponse response, DeviceFile deviceFile) throws Exception {
    	if (deviceFile != null) {
			if(StringUtil.isEmpty(deviceFile.getOutTime())){
				deviceFile.setOutTime(null);
			}
			if(StringUtil.isEmpty(deviceFile.getUseTime())){
				deviceFile.setUseTime(null);
			}
		}
        Long affectRow = deviceFileService.editDeviceFileById(deviceFile);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 分页查询设备档案
     * @param response
     * @param criteria 分页查询条件
     */
    @RequestMapping("/queryDeviceFile")
    public void queryDeviceFile(HttpServletResponse response, LzQueryCriteria criteria){
        String keyword01 = criteria.getKeyword01();
        String keyword02 = criteria.getKeyword02();
        if(keyword01 == null){
            keyword01 = "";
        }
        if(keyword02 == null){
            keyword02 = "";
        }
        criteria.setKeyword01(keyword01);
        criteria.setKeyword02(keyword02);
        List<DeviceAccount> deviceList = deviceFileService.queryDeviceFile(criteria);
        int count = deviceFileService.countDeviceFile(criteria);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(deviceList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据当前设备ID查询与当前设备有关的附属设备
     * @param response
     * @param dId 当前设备ID
     */
    @RequestMapping("/queryAttachDetailById")
    public void queryAttachDetailById(HttpServletResponse response, String dId) throws Exception {
        List<DeviceFileAttach> deviceFileAttach = deviceFileService.queryAttachDetailById(Long.parseLong(dId));
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(deviceFileAttach);
        if (deviceFileAttach.size() > AFFECT_ROW){
            jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", jsonArray);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
        } else {
            jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", null);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
        }
    }

    /**
     * 根据当前设备ID查询与当前设备有关的检修详情
     * @param response
     * @param dId 当前设备ID
     */
    @RequestMapping("/queryDeviceFileOverhaulById")
    public void queryDeviceFileOverhaulById(HttpServletResponse response, String dId) throws Exception {
        List<DeviceFileOverhaul> deviceFileOverhaul = deviceFileService.queryDeviceFileOverhaulById(Long.parseLong(dId));
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(deviceFileOverhaul);
        if (deviceFileOverhaul.size() > AFFECT_ROW){
            jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", jsonArray);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
        } else {
            jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", null);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
        }
    }

    /**
     * 根据当前设备ID查询与当前设备有关的润滑详情
     * @param response
     * @param dId 当前设备ID
     */
    @RequestMapping("/queryDeviceFileLubricateById")
    public void queryDeviceFileLubricateById(HttpServletResponse response, String dId) throws Exception {
        List<DeviceFileLubricate> deviceFileLubricate = deviceFileService.queryDeviceFileLubricateById(Long.parseLong(dId));
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(deviceFileLubricate);
        if (deviceFileLubricate.size() > AFFECT_ROW){
            jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", jsonArray);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
        } else {
            jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", null);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
        }
    }
}
